<?php
namespace app\index\controller;
use app\model\Category as CategoryModel;
use think\Db;


class Index extends Base
{
    public function __construct(){
        $model = new CategoryModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '首页';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function contact(){
        return view('contact', [
      
        ]);
    }

    public function index(){
        // $banner = Db::name('banner')->where([
        //     ['status','=',1],
        //     ['location','=',0]
        // ])->order('sort desc,id asc')->select();

        // $dynamic = Db::name('dynamic')->where([
        //     ['status','=',1],
        // ])->order('sort desc,id asc')->select();

        // // $product = Db::name('product')->where([
        // // ])->order('sort desc,id asc')->select();

        // $articles = Db::name('article')->field('content',true)->limit(4)->select();

        // $yscs = [];
        // for($i = 1;$i <= 4;$i++) {
        //     $yscs[] = Db::name('ysc')->where('group',$i)->limit(6)->select();
        // }

        // return view('index', [
        //     'banner'=>$banner,
        //     'dynamic'=>$dynamic,
        //     // 'product'=>$product,
        //     'articles'=>$articles,
        //     'yscs'=>$yscs
        // ]);

        $module1 = Db::name('module1')->find();
        $module1['content'] = explode("\n",$module1['content']);

        $module2 = Db::name('module2')->find();
        $module2['content'] = explode("\n",$module2['content']);
        foreach ($module2['content'] as $k => $vo) {
            $module2['content'][$k] = explode(',',$vo);
        }

        $dynamic = Db::name('dynamic')->where([
            ['status','=',1],
        ])->order('sort desc,id asc')->limit(6)->select();
        $dynamic = array_chunk($dynamic, 3);

        $articles = Db::name('article')->field('content',true)->limit(4)->select();

        $product = Db::name('product')->where([
        ])->order('sort desc,id asc')->select();

        $partners = Db::name('partners')->order('sort desc,id asc')->limit(6)->select();

        return view('index', [
            'module1'=>$module1,
            'module2'=>$module2,
            'dynamic'=>$dynamic,
            'articles'=>$articles,
            'product'=>$product,
            'partners'=>$partners,
        ]);
    }

    public function activity(){
        $activitys = Db::name('activity')->field('content',true)->limit(4)->select();
        return view('activity', [
            'activitys'=>$activitys,
            'banners'=>1
        ]);
    }

    public function search(){
        $q = input('q');
        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',12)
        ];
        $list = Db::name('search_view')->where($map)->where('title|description','like','%'.$q.'%')->paginate($page_param);
        $page = $list->render();
        $page = indexPage($page);

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            if (!empty($q)) {
                $list['data'][$k]['title'] = str_replace($q,'<strong>'.$q.'</strong>',$vo['title']);
                $list['data'][$k]['description'] = str_replace($q,'<strong>'.$q.'</strong>',$vo['description']);
            }

            if ($vo['type'] == 1) {
                $list['data'][$k]['category_name'] = Db::name('category')->where('id',$vo['category_id'])->value('name');
                $list['data'][$k]['category_url'] = url('category/index',['id'=>$ov['category_id']]);
                $list['data'][$k]['url'] = url('product/detail',['id'=>$vo['id']]);
            }elseif ($vo['type'] == 2) {
                $list['data'][$k]['category_name'] = '最新咨询';
                $list['data'][$k]['category_url'] = url('article/index');
                $list['data'][$k]['url'] = url('article/detail',['id'=>$vo['id']]);
            }elseif ($vo['type'] == 3) {
                $list['data'][$k]['category_name'] = '最新活动';
                $list['data'][$k]['category_url'] = url('index/activity');
                $list['data'][$k]['url'] = url('activity/detail',['id'=>$vo['id']]);
            }
        }

        return view('search', [
            'q'=>$q,
            'list'=>$list,
            'page'=>$page
        ]);
    }

    
}
